﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.model;
using InHD.commands;
using System.Windows.Input;
using InHD.dataaccess;

namespace InHD.viewmodels
{
    class ProductsListViewModel : WorkspaceViewModel
    {
        #region Private variables
        private Product _product;
        private List<Product> _products;
        private ProductRepository _productRepository;
        private int _rowIndex = -1;
        #endregion

        #region Constructor
        public ProductsListViewModel()
        {
        }

        public ProductsListViewModel(List<Product> ps)
        {
            this._products = ps;
        }

        #endregion

        #region Public properties
        public List<Product> ProductRowCollection
        {
            get
            {
                return this._products;
            }
            set
            {
                if (this._products != value)
                {
                    this._products = value;
                    OnPropertyChanged("ProductRowCollection");
                }
            }
        }

        public int RowIndex
        {
            get
            {
                return this._rowIndex;
            }
            set
            {
                if (value != _rowIndex)
                {
                    this._rowIndex = value;
                    OnPropertyChanged("RowIndex");
                }
            }
        }

        public Product SelectedProduct
        {
            get
            {
                return this._product;
            }
            set
            {
                if (value != null)
                {
                    this._product = value;
                }
            }
        }

        #endregion

        #region Commands
        private RelayCommand _viewProductCommand;
        public ICommand ViewProductCommand
        {
            get
            {
                if (_viewProductCommand == null)
                {
                    _viewProductCommand = new RelayCommand(
                        (param) => this.ViewProduct(), (param) => this.CanView);
                }
                return _viewProductCommand;
            }
        }

        private RelayCommand _addNewProductCommand;
        public ICommand AddNewProductCommand
        {
            get
            {
                if (_addNewProductCommand == null)
                {
                    _addNewProductCommand = new RelayCommand(
                        (param) => this.AddNewProduct());
                }
                return _addNewProductCommand;
            }
        }
        #endregion

        #region Private helpers
        private void ViewProduct()
        {
            commandExcute = "View";
            OnRequestClose();
        }

        private void AddNewProduct()
        {
            commandExcute = "Add";
            OnRequestClose();
        }
        #endregion

        public string commandExcute { get; set; }

        public bool CanView { get { return (this.SelectedProduct == null) ? false : true; } }
    }
}
